using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using System.Xml;
using NVelocityTemplateEngine.Interfaces;
using System.Collections;
using System.Reflection;
using NVelocityTemplateEngine;

namespace WorkFlowTests
{
    public class VelocityTemplates
    {
        [Fact(Skip="true")]
        public void Test1()
        {
            string ruleXML = GenerateRuleXML();

            //compare the generated output with the expected output
            Assert.True(WellFormedXML(ruleXML), "Xml is not well formed. The parser through an exception");

            //TODO: do a schema validation using the xsd to check if the document is conformant.
        }

        private string GenerateRuleXML()
        {

            string assemblyName = "TestWorkFlowLibrary";

            INVelocityEngine embeddedEngine = NVelocityEngineFactory.CreateNVelocityAssemblyEngine(assemblyName,"EventAbstractionAPI.RuleEngine", true);


            IDictionary context = new Hashtable();

            //$EventType1 = 2
            //$EventType2 = 3
            //$ComplexEvent = 1
            context.Add("EventType1", "eventType2");
            context.Add("EventType2", "eventType3");
            context.Add("ComplexEvent", "eventType1");

            String output = embeddedEngine.Process(context, "Resources.SequenceRule.vm");

            return output;
        }

        private bool WellFormedXML(string documentString)
        {
            XmlDocument doc = new XmlDocument();

            try 
	        {	        
        		doc.LoadXml(documentString);
	        }
	        catch (XmlException)
	        {
                //document is not well formed as it didn't pass the xml parsing.
                return false;
	        }

            //document is well formed to XML.
            return true;
        }
    }
}
